﻿#region License
// Copyright 2008 Doug Mayer - http://code.google.com/p/nfaker
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace NFaker
{
	public static class RandomExtensions
	{
		/// <summary>
		/// Get a random value from <paramref name="values"/>,
		/// or the default value if <paramref name="values"/> is empty.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="values">The array with a random value to select.</param>
		public static T Random<T>(this T[] values)
		{
			if (values.Length == 0) return default(T);

			return values[Fake.Random.Next(0, values.Length - 1)];
		}

		/// <summary>
		/// Shuffles the given array in place.  The specified <paramref name="values"/>
		/// array will be ordered randomly.
		/// </summary>
		/// <param name="values">The array to reorder.</param>
		/// <returns>The given array after it has been randomly reordered.</returns>
		public static T[] ShuffleInPlace<T>(this T[] values)
		{
			for (int i = values.Length - 1; i > 0; --i)
			{
				int position = Fake.Random.Next(i);
				T temp = values[i];
				values[i] = values[position];
				values[position] = temp;
			}
			return values;
		}
	}
}